package com.baidu.mapapi;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import java.io.InputStream;

public class MyLocationOverlay extends Overlay
  implements SensorEventListener, LocationListener, Overlay.Snappable
{
  private Location a = null;
  private GeoPoint b = null;
  private GeoPoint c = null;
  private Runnable d = null;
  private SensorManager e = null;
  private MapView f = null;
  private Bitmap g = null;
  private Bitmap h = null;
  private Bitmap i = null;
  private boolean j = false;
  private boolean k = false;
  private float l = (0.0F / 0.0F);
  private Paint m = null;

  public MyLocationOverlay(Context paramContext, MapView paramMapView)
  {
    if (paramMapView == null)
      throw new IllegalArgumentException("mapView is null");
    this.f = paramMapView;
    this.e = ((SensorManager)paramContext.getSystemService("sensor"));
    this.m = new Paint();
    this.m.setARGB(35, 0, 0, 128);
    this.m.setStyle(Paint.Style.FILL_AND_STROKE);
    this.m.setAntiAlias(true);
    try
    {
      AssetManager localAssetManager = paramContext.getAssets();
      InputStream localInputStream1 = localAssetManager.open("icon_my.png");
      this.g = BitmapFactory.decodeStream(localInputStream1);
      localInputStream1.close();
      InputStream localInputStream2 = localAssetManager.open("compass_bg.png");
      this.h = BitmapFactory.decodeStream(localInputStream2);
      localInputStream2.close();
      InputStream localInputStream3 = localAssetManager.open("compass_pointer.png");
      this.i = BitmapFactory.decodeStream(localInputStream3);
      localInputStream3.close();
      return;
    }
    catch (Exception localException)
    {
    }
  }

  protected GeoPoint GetBaidu09Point(GeoPoint paramGeoPoint, int paramInt)
  {
    GeoPoint localGeoPoint;
    if (paramGeoPoint == null)
      localGeoPoint = null;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return localGeoPoint;
          switch (paramInt)
          {
          default:
            localGeoPoint = paramGeoPoint;
          case 0:
          case 1:
          }
        }
        localGeoPoint = CoordinateConvert.bundleDecode(CoordinateConvert.fromWgs84ToBaidu(paramGeoPoint));
      }
      localGeoPoint = CoordinateConvert.bundleDecode(CoordinateConvert.fromGcjToBaidu(paramGeoPoint));
    }
  }

  public void disableCompass()
  {
    this.k = false;
    if (this.e != null)
    {
      Sensor localSensor = this.e.getDefaultSensor(3);
      if (localSensor != null)
        this.e.unregisterListener(this, localSensor);
    }
    this.f.b.b = 0;
    this.f.invalidate();
  }

  public void disableMyLocation()
  {
    this.j = false;
    this.a = null;
    this.b = null;
    this.c = null;
    Mj.b.removeUpdates(this);
    this.f.invalidate();
  }

  protected boolean dispatchTap()
  {
    return false;
  }

  public boolean draw(Canvas paramCanvas, MapView paramMapView, boolean paramBoolean, long paramLong)
  {
    int i1;
    if (paramBoolean)
      i1 = 0;
    while (true)
    {
      return i1;
      if ((this.a != null) && (this.b != null) && (this.c != null))
        drawMyLocation(paramCanvas, paramMapView, this.a, this.c, paramLong);
      if (this.k)
        drawCompass(paramCanvas, this.l);
      i1 = 1;
    }
  }

  protected void drawCompass(Canvas paramCanvas, float paramFloat)
  {
    if ((paramFloat > 360.0F) || (paramFloat < -360.0F));
    while (true)
    {
      return;
      paramCanvas.drawBitmap(this.h, 10, 10, null);
      int i1 = 10 + this.i.getWidth() / 2;
      int i2 = 10 + this.i.getHeight() / 2;
      Matrix localMatrix = new Matrix();
      localMatrix.postTranslate(10, 10);
      localMatrix.postRotate(-paramFloat, i1, i2);
      Paint localPaint = new Paint();
      localPaint.setAntiAlias(true);
      paramCanvas.drawBitmap(this.i, localMatrix, localPaint);
    }
  }

  protected void drawMyLocation(Canvas paramCanvas, MapView paramMapView, Location paramLocation, GeoPoint paramGeoPoint, long paramLong)
  {
    if (paramGeoPoint == null);
    while (true)
    {
      Point localPoint;
      do
      {
        return;
        localPoint = paramMapView.getProjection().toPixels(paramGeoPoint, null);
        int i1 = localPoint.x - this.g.getWidth() / 2;
        int i2 = localPoint.y - this.g.getHeight() / 2;
        paramCanvas.drawBitmap(this.g, i1, i2, null);
      }
      while (!(paramLocation.hasAccuracy()));
      float f1 = paramLocation.getAccuracy();
      float f2 = paramMapView.getProjection().metersToEquatorPixels(f1);
      paramCanvas.drawCircle(localPoint.x, localPoint.y, f2, this.m);
    }
  }

  public boolean enableCompass()
  {
    if (this.e != null)
    {
      Sensor localSensor = this.e.getDefaultSensor(3);
      if (localSensor != null)
        this.k = this.e.registerListener(this, localSensor, 3);
    }
    return this.k;
  }

  public boolean enableMyLocation()
  {
    Mj.b.requestLocationUpdates(this);
    this.a = Mj.b.getLocationInfo();
    if (this.a != null)
    {
      this.b = new GeoPoint(this.a.getLatitude(), this.a.getLongitude());
      this.c = GetBaidu09Point(this.b, Mj.c);
    }
    this.j = true;
    this.f.invalidate();
    return true;
  }

  public Location getLastFix()
  {
    return this.a;
  }

  public GeoPoint getMyLocation()
  {
    return this.b;
  }

  public float getOrientation()
  {
    return this.l;
  }

  public boolean isCompassEnabled()
  {
    return this.k;
  }

  public boolean isMyLocationEnabled()
  {
    return this.j;
  }

  public void onAccuracyChanged(Sensor paramSensor, int paramInt)
  {
  }

  public void onLocationChanged(Location paramLocation)
  {
    this.a = paramLocation;
    if (this.a == null)
    {
      this.b = null;
      this.c = null;
      this.f.invalidate();
    }
    while (true)
    {
      do
      {
        return;
        this.b = new GeoPoint(paramLocation.getLatitude(), paramLocation.getLongitude());
        this.c = GetBaidu09Point(this.b, Mj.c);
        this.f.invalidate();
      }
      while (this.d == null);
      this.d.run();
      this.d = null;
    }
  }

  public void onProviderDisabled(String paramString)
  {
    this.j = false;
  }

  public void onProviderEnabled(String paramString)
  {
    this.j = true;
  }

  public void onSensorChanged(SensorEvent paramSensorEvent)
  {
    if (paramSensorEvent.sensor.getType() == 3)
    {
      int i1 = (int)paramSensorEvent.values[0];
      this.l = i1;
      this.f.b.b = i1;
      this.f.invalidate();
    }
  }

  public boolean onSnapToItem(int paramInt1, int paramInt2, Point paramPoint, MapView paramMapView)
  {
    return false;
  }

  public boolean onTap(GeoPoint paramGeoPoint, MapView paramMapView)
  {
    boolean bool;
    Point localPoint1;
    GeoPoint localGeoPoint = getMyLocation();
    if (localGeoPoint != null)
    {
      localPoint1 = this.f.getProjection().toPixels(localGeoPoint, null);
      if (this.g == null)
        bool = false;
    }
    while (true)
    {
      while (true)
      {
        return bool;
        int i1 = this.g.getWidth() / 2;
        int i2 = this.g.getHeight() / 2;
        Rect localRect = new Rect(localPoint1.x - i1, localPoint1.y - i2, i1 + localPoint1.x, i2 + localPoint1.y);
        Point localPoint2 = this.f.getProjection().toPixels(paramGeoPoint, null);
        if (!(localRect.contains(localPoint2.x, localPoint2.y)))
          break;
        bool = dispatchTap();
      }
      bool = false;
    }
  }

  public boolean runOnFirstFix(Runnable paramRunnable)
  {
    int i1;
    if (this.a != null)
    {
      paramRunnable.run();
      i1 = 1;
    }
    while (true)
    {
      return i1;
      this.d = paramRunnable;
      i1 = 0;
    }
  }
}